package com.link.base.base.user.service;

import com.google.gson.Gson;
import com.link.base.base.user.model.UserConfig;
import com.link.base.base.user.model.UserTableConfig;
import com.link.base.core.basic.util.YmlConstants;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户自定义配置
 *
 * @author 韦胜健
 * @date 2019/2/18 11:56
 */
@Service
public class UserConfigServiceImpl implements UserConfigService {

    /*文件编码格式*/
    private static final String ENCODING = "UTF-8";

    /*使用gson生成以及解析json字符串*/
    private Gson gson = new Gson();

    /**
     * 获取当前用户个性化配置文件
     * @author  韦胜健
     * @date    2019/12/19 19:42
     */
    @Override
    public File getUserConfigJsonFile() throws Exception {
        CoreUser user = UserUtil.getUser();
        String configFilePath = YmlConstants.getPortalShareFileDir() + "/web/file/config/" + user.getUsername() + "/config.json";
        File configFile = new File(configFilePath);
        if (configFile.exists()) {
            return configFile;
        }
        File parentFile = configFile.getParentFile();
        if (!parentFile.exists()) {
            Boolean mkdirsFlag = parentFile.mkdirs();
            if (!mkdirsFlag) {
                throw new ServiceException("USER-070", parentFile.getPath());
            }
        }
        Boolean createNewFileFlag = configFile.createNewFile();
        if (!createNewFileFlag) {
            throw new ServiceException("USER-070", parentFile.getPath());
        }
        return configFile;
    }

    /**
     * 获取当前用户个性化配置json字符串
     * @author  韦胜健
     * @date    2019/12/19 19:43
     */
    @Override
    public String getUserConfigJsonString() throws Exception {
        File configFile = this.getUserConfigJsonFile();
        return FileUtils.readFileToString(configFile, ENCODING);
    }

    /**
     * 获取当前用户个性化配置信息对象
     * @author  韦胜健
     * @date    2019/12/19 19:43
     */
    @Override
    public UserConfig getUserConfig() throws Exception {
        String jsonString = this.getUserConfigJsonString();
        UserConfig userConfig = new Gson().fromJson(jsonString, UserConfig.class);
        if (userConfig == null) {
            userConfig = new UserConfig();
        }
        return userConfig;
    }

    /**
     * 保存当前用户个性化配置信息到文件
     * @author  韦胜健
     * @date    2019/12/19 19:43
     */
    @Override
    public void saveUserConfig(UserConfig userConfig) throws Exception {
        File configFile = this.getUserConfigJsonFile();
        String configJson = gson.toJson(userConfig);
        System.out.println("configJson" + configJson);
        FileUtils.writeStringToFile(configFile, configJson, ENCODING);
    }

    /**
     * 清空当前用户个性化配置信息数据
     * @author  韦胜健
     * @date    2019/12/19 19:44
     */
    @Override
    public void deleteUserConfig() throws Exception {
        File configFile = this.getUserConfigJsonFile();
        FileUtils.deleteQuietly(configFile);
    }

    /**
     * 保存用户个性化主题色
     * @author  韦胜健
     * @date    2019/12/19 19:44
     */
    @Override
    public void saveThemeColor(String color) throws Exception {
        UserConfig userConfig = this.getUserConfig();
        userConfig.setThemeColor(color);
        this.saveUserConfig(userConfig);
    }

    /**
     * 保存当前用户个性化设置表格的信息
     * @author  韦胜健
     * @date    2019/12/19 19:44
     */
    @Override
    public UserTableConfig saveTableConfig(UserTableConfig userTableConfig) throws Exception {
        UserConfig userConfig = this.getUserConfig();
        Map<String, UserTableConfig> map = userConfig.getTableConfigs();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(userTableConfig.getId(), userTableConfig);
        userConfig.setTableConfigs(map);
        this.saveUserConfig(userConfig);
        return userTableConfig;
    }

    /**
     * 删除当前用户个性化设置表格的信息
     * @author  韦胜健
     * @date    2019/12/19 19:45
     */
    @Override
    public void deleteTableConfig(UserTableConfig userTableConfig) throws Exception {
        UserConfig userConfig = this.getUserConfig();
        Map<String, UserTableConfig> map = userConfig.getTableConfigs();
        if (map == null) {
            map = new HashMap<>();
        }
        map.remove(userTableConfig.getId());
        userConfig.setTableConfigs(map);
        this.saveUserConfig(userConfig);
    }

}
